package com.qf.common.util;

import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

public class ResultInfo extends HashMap<String,Object> {
    public ResultInfo(){
        put("code",HttpStatus.SC_OK);
        put("msg","success");
    }

    public static ResultInfo error(){
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR,"未知异常，请联系管理员");
    }

    public static ResultInfo error(String msg){
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR,msg);
    }

    public static ResultInfo error(int code, String message) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.put("code",code);
        resultInfo.put("msg",message);
        return resultInfo;
    }

    public static ResultInfo ok(String msg){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.put("code",HttpStatus.SC_OK);
        resultInfo.put("msg",msg);
        return resultInfo;
    }

    public static ResultInfo ok(Map<String,Object> map){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.putAll(map);
        return resultInfo;
    }

    public boolean isSuccess() {
        return getCode() == HttpStatus.SC_OK;
    }

    public static ResultInfo ok(){
        return new ResultInfo();
    }

    public ResultInfo setData(Object value) {
        super.put("data", value);
        return this;
    }

    public ResultInfo put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Integer getCode() {
        return (Integer) this.get("code");
    }

    public String getMsg() {
        return (String) this.get("msg");
    }
}
